00:02
Functions are a great
solution when you have
00:06
similar code that you want
00:07
to reuse in
different places.
00:10
This code, I
have a program
00:13
that asks the user for
the length and width
00:15
of a rectangle and
multiplies them
00:17
together to get the area
00:19
and displays it
on the screen.
00:20
Now, it doesn't make sense
00:22
for either of
these values,
00:24
the length or the
width to be negative.
00:27
if we had some
code in there to
00:29
detect a negative value
00:30
and ask the user
for a new one.
00:32
And we could add code
00:34
for each one to
check it separately,
00:37
pretty similar
to each other.
00:39
So here's an
example doing that.
00:41
And you can see that
00:43
this loop here
that checks,
00:45
and this loop here that
00:47
checks is actually
very similar.
00:50
And so whenever we
want to accomplish
00:53
the same thing
or something
00:55
very similar in more
00:56
than one place
in our program,
00:58
it's a great example
01:00
of when a function
could help.
01:02
In this case, we could
01:03
create a function to get
01:05
a positive value from
01:06
the user and re
prompt them,
01:08
if necessary, and
then return it.
01:10
So let's see
how we could do
01:12
this back in our
original program.
01:14
First, let's
define a function
01:17
at the top of the program.
01:18
A good name for
this function might
01:20
be get positive value.
01:25
Now, notice that when
we name variables,
01:30
we use nouns because
variables are things.
01:33
But when we choose names
for our functions,
01:36
we usually use verbs
01:37
because functions
do things.
01:40
So, in this case, get
01:41
positive value
is better than
01:44
just calling it
positive value
01:46
or positive or
something like that.
01:48
The gt helps us know
01:49
that it's going
to do something.
01:51
Okay, next, let's
add a doc string.
01:55
my colon and my
dock string,
01:58
what this
function will do.
01:59
It will prompt
the user for
02:05
them if the original value
02:10
is negative.
Something like that.
02:14
Now, we could add
some comments here
02:17
in our function to kind of
02:18
plan out what
we're going to do.
02:19
So the first thing I might
02:21
do is prompt
for the value.
02:24
Then I might check if
02:27
the value is positive
02:33
and re prompt, if needed.
02:38
return the value
that we got.
02:42
that we need to
consider is,
02:43
what does this function
02:45
need in order
to do its job?
02:48
In this case, the text
02:51
that we show to
the user for the
02:53
prompt needs
to actually be
02:55
different depending on
02:56
the value that
it's asking for,
02:57
because in one
case, it said,
02:59
what is the length,
and in the other,
03:01
it asks for the width.
03:03
So we can add this text
03:05
as a parameter up here,
03:07
and maybe I'll call
it prompt text.
03:10
whenever someone
calls the function,
03:12
they can just pass that
in as a parameter.
03:19
to fill in the details
of this function.
03:21
To prompt for the value,
03:23
we'll say value
is float input.
03:28
And then here is where I'm
03:30
just going to
give prompt text,
03:32
whatever they passed
in as that parameter.
03:35
Then to check if it's
03:37
positive and re prompt,
I'll say while,
03:40
value is less than zero,
03:44
then maybe we print
a message that says,
03:47
sorry, the value
cannot be negative.
03:52
And we try again.
Value is float input,
03:57
prompt text, and we can
03:59
use that variable
one more time.
04:00
Now, if we wanted to
make this even fancier,
04:03
instead of saying
the value,
04:04
we want to say the length,
04:06
we could add another
parameter up
04:07
here for the actual
name of that variable,
04:10
and we could do this
in different ways.
04:12
But for now, I'll keep
it really simple,
04:13
just letting them pass in
04:15
the whole prompt
text in one string.
04:18
So then once this is done,
04:21
we can return the value.
04:24
Okay. So now,
let's go ahead
04:28
and change our code down
04:29
here to call the function.
04:31
So here, I can now
change this to say
04:35
the length is get
positive value.
04:40
And then what
does it need?
04:42
Well, it needs that string
04:43
there that was
the prompt text.
04:45
Make sure that my
parentheses match.
04:48
And then I'll do the
same thing here.
04:53
Make sure my
parentheses match.
04:56
I need one more
right there.
05:02
run this and see
how it works.
05:04
Let's go ahead and run
this with debugging.
05:10
And I'll start
stepping over.
05:12
So right here, I'm going
05:16
value first for
the length.
05:18
So let's step into
the function and see,
05:22
that length question
that I'm interested in.
05:27
for the length of
the rectangle.
05:29
Let's say that I put in a
05:30
negative number
like negative five.
05:33
Now, because it
was negative,
05:37
I see the error message,
05:41
better number this
time like four.
05:43
It checks, and it
returns the value back,
05:47
and now length has
that value four.
05:51
Then I step in
one more time,
05:55
displays what is the
width of the rectangle.
05:58
And so I can give
this maybe ten.
06:01
Checks this, re prompts,
06:03
if needed, it
returns it back.
06:05
And so now I have both
06:07
the length and
the width here,
06:10
we can multiply
and display.
06:16
able to make this
function up here,
06:17
which maybe had some
complicated logic.
06:20
But then throughout the
rest of my program,
06:22
it's really easy for me to
06:24
reuse it anytime
that I need to.
06:27
So when you write
functions like this,
06:29
there are a lot
of benefits.
06:32
You avoid having
to write it
06:33
twice, but more
importantly,
06:36
if you want to add or fix
06:38
anything later in the
logic of that function,
06:41
you only need to fix it
06:42
and test it in one place.
06:44
This is really important.
06:46
And using functions like
06:48
this can help you write
06:50
larger programs
and make them
06:51
easier to fix and
improve later on.